package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.mybatis.Pagination;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.goods.GoodsBO;
import com.ymk.core.code.cmd.GoodsCmdCode;
import com.ymk.core.enums.goods.GoodsStatusEnum;
import com.ymk.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class GoodsListCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO reqData = super.getData(sign, GoodsBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("title", reqData.getTitle())
                .addParam("mchId", reqData.getMchId())
                .addParam("catId", reqData.getCatId())
                .addParam("status", reqData.getStatus())
                .addParam("loadCat", true)
                .addParam("loadMch", true)
                .addParam("loadSku",true)
                .setReturnStruct(sign.getReturnStruct());
        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<GoodsBO> respDataList = this.goodsService.listForPage(pagination);
        respSign.setPageInfo(pagination);
        respSign.setSource(super.getSource(respDataList));

        return respSign;
    }

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO reqData = super.getData(sign, GoodsBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mchId",sign.getMchId())
                .addParam("status",reqData.getStatus())
                .addParam("catId",reqData.getCatId())
                .addParam("title",reqData.getTitle())
                .addParam("loadCat",true)
                .setReturnStruct(sign.getReturnStruct());
        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<GoodsBO> respDataList = this.goodsService.listForPage(pagination);
        respSign.setPageInfo(pagination);
        respSign.setSource(super.getSource(respDataList));

        return respSign;
    }

    @Override
    public CmdSign executeStore(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO reqData = super.getData(sign, GoodsBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mchId",sign.getMchId())
                .addParam("status", GoodsStatusEnum.NORMAL.getStatus())
                .addParam("catId",reqData.getCatId())
                .addParam("title",reqData.getTitle())
                .addParam("loadCat",true);

        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<GoodsBO> respDataList = this.goodsService.listForPage(pagination);
        respSign.setPageInfo(pagination);
        respSign.setSource(super.getSource(respDataList));

        return respSign;
    }

    @Override
    public String getCmdCode() {
        return GoodsCmdCode.GOODS_LIST;
    }

    @Autowired
    private IGoodsService goodsService;
}